package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysUserDo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysUserServiceImpl implements ISysUserService {
    private final SysUserMapper sysUserMapper;

    /**
     * 查询用户信息
     */
    @Override
    public SysUserDo queryById(Long userId) {
        return sysUserMapper.selectById(userId);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<SysUserDo> queryPageList(SysUserDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUserDo> lqw = buildQueryWrapper(entity);
        Page<SysUserDo> result = sysUserMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<SysUserDo> queryList(SysUserDo entity) {
        LambdaQueryWrapper<SysUserDo> lqw = buildQueryWrapper(entity);
        return sysUserMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysUserDo> buildQueryWrapper(SysUserDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysUserDo> lqw = Wrappers.lambdaQuery();
        lqw.eq(entity.getDeptId() != null, SysUserDo::getDeptId, entity.getDeptId());
        lqw.like(StringUtils.isNotBlank(entity.getUserName()), SysUserDo::getUserName, entity.getUserName());
        lqw.like(StringUtils.isNotBlank(entity.getNickName()), SysUserDo::getNickName, entity.getNickName());
        lqw.eq(StringUtils.isNotBlank(entity.getUserType()), SysUserDo::getUserType, entity.getUserType());
        lqw.eq(StringUtils.isNotBlank(entity.getEmail()), SysUserDo::getEmail, entity.getEmail());
        lqw.eq(StringUtils.isNotBlank(entity.getPhonenumber()), SysUserDo::getPhonenumber, entity.getPhonenumber());
        lqw.eq(StringUtils.isNotBlank(entity.getSex()), SysUserDo::getSex, entity.getSex());
        lqw.eq(StringUtils.isNotBlank(entity.getAvatar()), SysUserDo::getAvatar, entity.getAvatar());
        lqw.eq(StringUtils.isNotBlank(entity.getPassword()), SysUserDo::getPassword, entity.getPassword());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysUserDo::getStatus, entity.getStatus());
        lqw.eq(StringUtils.isNotBlank(entity.getLoginIp()), SysUserDo::getLoginIp, entity.getLoginIp());
        lqw.eq(entity.getLoginDate() != null, SysUserDo::getLoginDate, entity.getLoginDate());
        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insert(SysUserDo entity) {
        SysUserDo add = BeanUtil.toBean(entity, SysUserDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysUserMapper.insert(add) > 0;
        if (flag) {
            entity.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateById(SysUserDo entity) {
        SysUserDo update = BeanUtil.toBean(entity, SysUserDo.class);
        validEntityBeforeSave(update);
        return sysUserMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysUserDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysUserMapper.deleteBatchIds(ids) > 0;
    }
}
